package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import one.stand.code.RedisCode;
import one.stand.entity.ImageEntity;
import one.stand.entity.PosterEntity;
import one.stand.enums.LoginEnum;
import one.stand.model.BusinessCardModel;
import one.stand.model.CompanyAccountModel;
import one.stand.model.ResultModel;
import one.stand.request.BusinessCardRequest;
import one.stand.request.CompanyAccountRequest;
import one.stand.service.CompanyAccontService;
import one.stand.service.PosterService;
import one.stand.service.WxSunCodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: QIK
 * @CreateDate: 2019/6/26 9:15
 */
@Service
public class PosterServiceImpl extends PublicBaseService implements PosterService {
    @Autowired
    protected CompanyAccontService companyAccontService;
    @Autowired
    protected WxSunCodeService wxSunCodeService;
    @Override
    public ResultModel getCardPoster(BusinessCardRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        List<String> list = redisUtil.get(RedisCode.getCardPoster(request.getCardId()));
        if (list != null && list.size() > 0) {
            return ResultModel.success(list);
        }
        list = new ArrayList<>();

        BusinessCardModel model = businessCardMapper.select(new BusinessCardModel() {{
            setCardId(request.getCardId());
            setThumbsUserId(request.getId().intValue());
            setState(1);
        }});
        if (null == model) {
            log.info("未获取到名片信息");
            return ResultModel.fail("未获取到名片信息");
        }
        if (StringUtils.isBlank(model.getWxTwoCode())) {
            String wxTwoCode = getWxCode(request.getCardId().toString(), model.getCompanyId().toString());
            if (StringUtils.isBlank(wxTwoCode)) {
                return ResultModel.fail();
            }
            model.setWxTwoCode(wxTwoCode);
        }
        CompanyAccountModel companyAccountModel=companyAccontService.selectByCompanyKey(
                new CompanyAccountRequest(){{
                    setCompanyKey(request.getCompanyKey());
                    setAccountType(LoginEnum.CRM.getAccountType());
                }}
        );
        //list.add(card1(model,companyAccountModel.getAppName()));
        list.add(this.card2(model,companyAccountModel.getAppName()));
        list.add(this.card3(model,companyAccountModel.getAppName()));
        list.add(this.card4(model));
        list.add(this.card5(model));

        return ResultModel.success(list);
    }
    public void addCardList(String url,Integer cardId){
        List<String> list = redisUtil.get(RedisCode.getCardPoster(cardId));
        if(null==list||list.size()<=0){
            list=new ArrayList<>();
        }
        list.add(url);
        redisUtil.set(RedisCode.getCardPoster(cardId),list);
    }
   
    @Override
    @Async("asyncServiceExecutor")
    public String card1(BusinessCardModel model,String appName) {
        PosterEntity posterEntity = new PosterEntity();
        posterEntity.setImagePath(cardPosterConfig.getUrl1());
        posterEntity.setCompanyName(getObjectString(model.getCompanyName()));
        posterEntity.setEmail(getObjectString(model.getEmail()));
        posterEntity.setPhone(getObjectString(model.getPhone()));
        posterEntity.setCardName(getObjectString(model.getName()));
        posterEntity.setQRCodeUrl(getObjectString(model.getWxTwoCode()));
        posterEntity.setCardLogoUrl(getObjectString(model.getLogo()));
        posterEntity.setAppName(appName);
        InputStream inputStream = imageGenerateUtil.businessCardDraw1(posterEntity);
        String url=ossUtil.getOssUrl() + ossUtil.uploadObjectOSS(inputStream, "png");
        addCardList(url,model.getCardId());
        return url;
    }
   
    @Override
    @Async("asyncServiceExecutor")
    public String card2(BusinessCardModel model,String appName) {
        PosterEntity posterEntity = new PosterEntity();
        posterEntity.setCardName(getObjectString(model.getName()));
        posterEntity.setPosition(getObjectString(model.getPosition()));
        posterEntity.setPhone(getObjectString(model.getPhone()));
        posterEntity.setQRCodeUrl(getObjectString(model.getWxTwoCode()));
        posterEntity.setCardLogoUrl(StringUtils.isBlank(model.getLogo()) ? cardPosterConfig.getCardLogoDefaultUrl() : model.getLogo());
        posterEntity.setCompanyLogo(getObjectString(model.getCompanyLogo()));
        posterEntity.setAppName(appName);
        posterEntity.setImagePath(cardPosterConfig.getUrl2());
        posterEntity.setCard2BackUrl(cardPosterConfig.getUrl2back());
        InputStream inputStream = imageGenerateUtil.businessCardDraw2(posterEntity);
        String url= ossUtil.getOssUrl() + ossUtil.uploadObjectOSS(inputStream, "png");
        addCardList(url,model.getCardId());
        return url;
    }

   
    @Override
    @Async("asyncServiceExecutor")
    public String card3(BusinessCardModel model,String appName) {
        PosterEntity posterEntity = new PosterEntity();
        posterEntity.setQRCodeUrl(getObjectString(model.getWxTwoCode()));
        posterEntity.setCardLogoUrl(StringUtils.isBlank(model.getLogo()) ? cardPosterConfig.getCardLogoDefaultUrl() : model.getLogo());
        posterEntity.setCardName(getObjectString(model.getName()));
        posterEntity.setEmail(getObjectString(model.getEmail()));
        posterEntity.setImagePath(cardPosterConfig.getUrl3());
        posterEntity.setPhone(getObjectString(model.getPhone()));
        posterEntity.setAppName(appName);
        InputStream inputStream = imageGenerateUtil.businessCardDraw3(posterEntity);
        String url= ossUtil.getOssUrl() + ossUtil.uploadObjectOSS(inputStream, "png");
        addCardList(url,model.getCardId());
        return url;
    }
   
    @Override
    @Async("asyncServiceExecutor")
    public String card4(BusinessCardModel model){
        ImageEntity imageEntity=new ImageEntity();
        imageEntity.setParam(cardPosterConfig.getUrl4());
        List<ImageEntity> list=new ArrayList<>();
        ImageEntity i=new ImageEntity();
        i.setType(1);
        i.setParam(model.getCompanyName());
        i.setSize(36);
        i.setColor(new Color(56,56,56));
        i.setCentered(true);
        i.setYAxis(936+34);
        i.setPlain(Font.BOLD);
        list.add(i);

        ImageEntity i2=new ImageEntity();
        i2.setParam(model.getName());
        i2.setType(1);
        i2.setSize(36);
        i2.setColor(new Color(56,56,56));
        i2.setXAxis(81);
        i2.setYAxis(1060+34);
        i2.setPlain(Font.BOLD);
        list.add(i2);

        ImageEntity i3=new ImageEntity();
        i3.setParam(model.getPosition());
        i3.setType(1);
        i3.setSize(28);
        i3.setColor(new Color(120,120,120));
        i3.setXAxis(81);
        i3.setYAxis(1114+26);
        i3.setPlain(Font.PLAIN);
        list.add(i3);

        ImageEntity i4=new ImageEntity();
        i4.setParam(model.getPhone());
        i4.setType(1);
        i4.setSize(24);
        i4.setColor(new Color(168,168,168));
        i4.setXAxis(134);
        i4.setYAxis(1177+19);
        i4.setPlain(Font.PLAIN);
        list.add(i4);

        ImageEntity i5=new ImageEntity();
        i5.setParam(model.getLogo());
        i5.setType(2);
        i5.setWidth(120);
        i5.setHigh(120);
        i5.setXAxis(415);
        i5.setYAxis(1068);
        list.add(i5);

        ImageEntity i6=new ImageEntity();
        i6.setParam(model.getWxTwoCode());
        i6.setType(2);
        i6.setWidth(120);
        i6.setHigh(120);
        i6.setXAxis(555);
        i6.setYAxis(1068);
        list.add(i6);
        imageEntity.setImageEntityList(list);
        InputStream inputStream = imageGenerateUtil.image(imageEntity);
        String url= ossUtil.getOssUrl() + ossUtil.uploadObjectOSS(inputStream, "png");
        addCardList(url,model.getCardId());
        return url;
    }
   
    @Override
    @Async("asyncServiceExecutor")
    public String card5(BusinessCardModel model){
        ImageEntity imageEntity=new ImageEntity();
        imageEntity.setParam(cardPosterConfig.getUrl5());
        List<ImageEntity> list=new ArrayList<>();
        ImageEntity i=new ImageEntity();
        i.setParam(model.getLogo());
        i.setType(2);
        i.setWidth(120);
        i.setHigh(120);
        i.setXAxis(40);
        i.setYAxis(983);
        list.add(i);

        ImageEntity i2=new ImageEntity();
        i2.setParam(model.getName());
        i2.setType(1);
        i2.setSize(36);
        i2.setColor(new Color(56,56,56));
        i2.setXAxis(48);
        i2.setYAxis(1128+34);
        i2.setPlain(Font.BOLD);
        list.add(i2);

        ImageEntity i3=new ImageEntity();
        i3.setParam(model.getPhone());
        i3.setType(1);
        i3.setSize(24);
        i3.setColor(new Color(120,120,120));
        i3.setXAxis(48);
        i3.setYAxis(1191+19);
        i3.setPlain(Font.PLAIN);
        list.add(i3);


        ImageEntity i4=new ImageEntity();
        i4.setParam(model.getEmail());
        i4.setType(1);
        i4.setSize(24);
        i4.setColor(new Color(120,120,120));
        i4.setXAxis(48);
        i4.setYAxis(1226+23);
        i4.setPlain(Font.PLAIN);
        list.add(i4);


        ImageEntity i5=new ImageEntity();
        i5.setParam(model.getWxTwoCode());
        i5.setType(2);
        i5.setWidth(184);
        i5.setHigh(184);
        i5.setXAxis(522);
        i5.setYAxis(1083);
        list.add(i5);
        imageEntity.setImageEntityList(list);
        InputStream inputStream = imageGenerateUtil.image(imageEntity);
        String url= ossUtil.getOssUrl() + ossUtil.uploadObjectOSS(inputStream, "png");
        addCardList(url,model.getCardId());
        return url;
    }

   
    @Override
    public String getWxCode(String cardId, String companyId) {
        if (StringUtils.isBlank(cardId)) {
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer("card=").append(cardId).append("&c=").append(companyId);
        Map<String, String> map = new HashMap<>(2);
        map.put("scene", stringBuffer.toString());
        map.put("page", cardSunCode);
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(map));
        CompanyAccountModel companyAccountModel = companyAccontService.selectByUserIdCopy(new CompanyAccountRequest() {{
            setCompanyId(Integer.parseInt(companyId));
            setAccountType(LoginEnum.USER.getAccountType());
        }});
        if (companyAccountModel != null) {
            String twoCode = wxSunCodeService.getWxCode(jsonObject, companyAccountModel.getAppId(), companyAccountModel.getAppSecret());
            log.info("获取到太阳码：{}", twoCode);
            if (StringUtils.isNotBlank(twoCode)) {
                businessCardMapper.updateByPrimaryKeySelective(new BusinessCardModel() {{
                    setCardId(Integer.parseInt(cardId));
                    setWxTwoCode(twoCode);
                }});
                return twoCode;
            }
        }
        return null;
    }

}
